import * as $auth from '@/api/auth'
import * as $user from '@/api/user'
import * as _wechat from '@/utils/wechat'
export const AUTH_SET_LOGINER = 'AUTH_SET_LOGINER'
export const AUTH_SET_IS_LOGINED = 'AUTH_SET_IS_LOGINED'
export const AUTH_SET_IS_NEED_AUTH_USER_INFO = 'AUTH_SET_IS_NEED_AUTH_USER_INFO'
export const AUTH_UPDATE_LOGINER_AVATAR_URL = 'AUTH_UPDATE_LOGINER_AVATAR_URL'
export const AUTH_UPDATE_LOGINER_NAME = 'AUTH_UPDATE_LOGINER_NAME'
export const AUTH_RESET_LOGINER = 'AUTH_RESET_LOGINER'

const state = () => {
  return {
    isNeedAuthUserInfo: false,
    loginer: {
      id: 0,
      avatarUrl: '',
      nickName: '',
      token: '',
      expiresIn: null,
    },
    isLogined: false,
  }
}

// getters
const getters = {
  isNeedAuthUserInfo: state => state.isNeedAuthUserInfo,
  loginer: state => state.loginer,
  isLogined (state) {
    return state.isLogined
  }
}

// actions
const actions = {
  async login ({ commit, state }) {
    let code = await _wechat.login()
    let rs = await $auth.login(code)
    if (!rs.access_token) {
      wx.showModal({title: '登录失败！'})
      return false
    }
    let loginer = {id: rs.user_id, avatarUrl: rs.avatar, token: rs.access_token, nickName: rs.nick_name, expiresIn: rs.expires_in}
    commit(AUTH_SET_LOGINER, loginer)
    commit(AUTH_SET_IS_LOGINED, true)
    getApp().globalData.token = loginer.token
    return true
  },
  async checkSession ({ commit, state }) {
    let flag = await _wechat.checkSession()
    if (!flag) return flag
    commit(AUTH_SET_IS_LOGINED, flag)
    return flag
  },
  async getUserInfo ({ commit, state }, {encryptedData, iv}) {
    let rs = await $auth.updateUserInfo({encryptedData, iv})
    if (!rs.access_token) {
      wx.showModal({title: '登录失败！'})
      return false
    }
    let loginer = {id: rs.user_id, avatarUrl: rs.avatar, token: rs.access_token, nickName: rs.nick_name, expiresIn: rs.expires_in}
    commit(AUTH_SET_LOGINER, loginer)
    commit(AUTH_SET_IS_NEED_AUTH_USER_INFO, false)
    getApp().globalData.token = loginer.token
  },
  setIsNeedAuthUserInfo ({ commit, state }, flag) {
    commit(AUTH_SET_IS_NEED_AUTH_USER_INFO, flag)
  },
  updateLoginerAvatarUrl ({ commit, state }, avatarUrl) {
    commit(AUTH_UPDATE_LOGINER_AVATAR_URL, avatarUrl)
  },
  updateLoginerName ({ commit, state }, name) {
    commit(AUTH_UPDATE_LOGINER_NAME, name)
  },
  resetLoginer ({ commit, state }) {
    commit(AUTH_RESET_LOGINER)
  },
  async saveBasicInfo ({ dispatch, commit, state }, {nickName, avatarUrl}) {
    let res = await $user.saveBasicInfo({nickName, avatarUrl})
    commit(AUTH_UPDATE_LOGINER_NAME, nickName)
    commit(AUTH_UPDATE_LOGINER_AVATAR_URL, avatarUrl)
    wx.navigateBack()
  },
}

// mutations
const mutations = {
  AUTH_SET_LOGINER (state, {id, avatarUrl, nickName, token, expiresIn}) {
    state.loginer = {id, avatarUrl, nickName, token, expiresIn}
  },
  AUTH_SET_IS_NEED_AUTH_USER_INFO (state, flag) {
    state.isNeedAuthUserInfo = flag
  },
  AUTH_SET_IS_LOGINED (state, flag) {
    state.isLogined = flag
  },
  AUTH_UPDATE_LOGINER_AVATAR_URL (state, avatarUrl) {
    state.loginer.avatarUrl = avatarUrl
  },
  AUTH_UPDATE_LOGINER_NAME (state, name) {
    state.loginer.nickName = name
  },
  AUTH_RESET_LOGINER (state) {
    state.loginer = {
      id: 0,
      avatarUrl: '',
      nickName: '',
      token: '',
      expiresIn: null,
    }
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
